﻿Imports SACommon

Public Class ucStart
    Private Sub ucStart_Load(sender As Object, e As EventArgs) Handles Me.Load
        CleanApplicationTable()
        LoadDefinedApplications()
    End Sub
    Sub CleanApplicationTable()
        Dim apps = gUDApp.ApplicationsList.ToList
        For Each item In apps
            If item.ApplicationTitle.Trim = "" Then gUDApp.remove(item)
        Next
    End Sub
    Sub LoadDefinedApplications()
        Dim cat As ListViewGroup
        Dim itm As ListViewItem
        'Reset menu
        lvwApplications.Groups.Clear()
        lvwApplications.Items.Clear()
        'Add items
        For Each citem In gCat.CategoryList
            cat = New ListViewGroup With {.Header = citem.CategoryDesc, .HeaderAlignment = HorizontalAlignment.Left}
            lvwApplications.Groups.Add(cat)
            Dim apps = gUDApp.ApplicationsList(citem.CategoryID)
            If apps.Count > 0 Then
                For Each item In apps
                    itm = New ListViewItem With {.ImageIndex = GetAppStatusImage(item), .Text = item.ApplicationTitle, .Tag = item.ApplicationID, .Group = cat}
                    lvwApplications.Items.Add(itm)
                Next
            End If
        Next
    End Sub
    Function GetAppStatusImage(AppItem As UpdateAppItem) As Integer
        Select Case gUDApp.GetAppStatus(AppItem)
            Case Is = UpdateApp.AppStatus.NewApp
                Return 0
            Case Is = UpdateApp.AppStatus.DefinedApp
                Return 1
            Case Is = UpdateApp.AppStatus.PublishedApp
                Return 2
        End Select
        Return 0
    End Function
    Private Sub lvwApplications_Click(sender As Object, e As EventArgs) Handles lvwApplications.Click
        If lvwApplications.SelectedItems.Count > 0 Then
            gUDApp.SelectedAppID = CInt(lvwApplications.SelectedItems(0).Tag)
            Messenger.SendMessage(Messenger.MessageType.AppSelected, "", True)
        End If
    End Sub
End Class
